package com.zhongxing.duan.spriteld;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zhongxing.duan.spriteld.util.HttpUtil;
import com.zhongxing.duan.spriteld.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import static android.widget.Toast.makeText;

/**
 * Created by Duan on 17/6/29.
 */

public class LoginActivity extends Activity implements View.OnClickListener {
    private Button btn_login;
    private Button btn_logout;
    private EditText et_account;
    private EditText et_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initUI();
    }

    /**
     * 初始化控件绑定
     */
    private void initUI() {
        btn_login = (Button) findViewById(R.id.btn_login);
        //添加点击事件
        btn_login.setOnClickListener(/*匿名内部类*/new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入框内容
                String account = et_account.getText().toString();
                String password = et_password.getText().toString();


                //数据合法性验证
                if (account.trim().length() < 5) {
                    ToastUtil.toast(getApplicationContext(), "账号至少要求5位");
                    return;
                }
                if (password.trim().length() < 3) {
                    ToastUtil.toast(getApplicationContext(), "密码至少要求3位");
                    return;
                }

                if ("admin".equals(account) && "123".equals(password)) {
                    //跳转到主界面
                    Intent intent = new Intent();//创建意图
                    /*
                        设置出发点及终点
                            出发点为Context类型，永远写getApplicationContext()
                            终点：为XXXActivity.class
                     */
                    intent.setClass(getApplicationContext(), MainActivity.class);
                    //执行意图
                    startActivity(intent);
                }

                //网络登录
                Map<String, String> map = new HashMap<String, String>();
                map.put("account", account);
                map.put("password", password);
                HttpUtil.requestPost("http://192.168.1.121:8080/SpriteWeb/login.do", map, new HttpUtil.HttpListener() {
                    @Override
                    public void result(String result) {
                        if (result.equals("success")) {
                            //跳转到主界面
                            Intent intent = new Intent();//创建意图
                    /*
                        设置出发点及终点
                            出发点为Context类型，永远写getApplicationContext()
                            终点：为XXXActivity.class
                     */
                            intent.setClass(getApplicationContext(), MainActivity.class);
                            //执行意图
                            startActivity(intent);
                        }
                    }
                });


            }
        });//代理模式
        btn_logout = (Button) findViewById(R.id.btn_logout);//继承的多态性
        //添加点击事件
        btn_logout.setOnClickListener(this);//接口的多态性

        et_account = (EditText) findViewById(R.id.et_account);
        et_password = (EditText) findViewById(R.id.et_password);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_logout:
                /*  Toast：吐司，会自动消失的提示框
                        makeText(context, text, duration)：创建Toast对象
                            context：上下文
                            text：显示的文本内容
                            duration：持续时间
                                Toast.LENGTH_SHORT  0：1秒左右
                                Toast.LENGTH_LONG   1：2秒左右
                        show()：显示提示框
                */
                makeText(getApplicationContext(), "登出", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
